package com.ruige.data.config;




import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author shenyu
 * @Description
 * @create 2020-3-12
 */
@Configuration
@MapperScan(basePackages = {"com.ruige.data.models.huapu.mapper","com.ruige.data.models.client.mapper"},sqlSessionFactoryRef = "hpdbSqlSessionFactory")
public class MybatisPlus1Config {

    @Resource(name = "routingDataSource")
    private DataSource routingDataSource;


    @Primary
    @Bean("hpdbSqlSessionFactory")
    public SqlSessionFactory hpdbSqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(routingDataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath*:mappers/ds1/**/*Mapper.xml"));
        sqlSessionFactory.setPlugins(new Interceptor[]{
                new PaginationInterceptor()
        });

        return sqlSessionFactory.getObject();
    }

    @Primary
    @Bean(name = "hpdbTransactionManager")
    public DataSourceTransactionManager hpdbTransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "hpdbSqlSessionTemplate")
    public SqlSessionTemplate hpdbSqlSessionTemplate(@Qualifier("hpdbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     *   mybatis-plus分页插件
     */
//    @Bean("1")
//    public PaginationInterceptor paginationInterceptor() {
//        PaginationInterceptor page = new PaginationInterceptor();
//        page.setDialectType("mysql");
//        return page;
//    }


}
